from pymongo import MongoClient


def get_Spider_DB():
    url = "192.168.1.99"
    # url = "localhost"
    port = 27017
    db_name = 'resultdb'
    client = MongoClient(url, port)
    client[db_name].authenticate('xmtj', '0-=p[]P{}', 'admin')
    db = client[db_name]
    return db


def get_Save_DB():
    url = "192.168.1.99"
    # url = "tit.pub"
    # url = "localhost"
    port = 27017
    db_name = 'comic_spider_db'
    client = MongoClient(url, port)
    client[db_name].authenticate('xmtj', '0-=p[]P{}', 'admin')
    db = client[db_name]
    return db


mongoDB = get_Spider_DB()
mongoSaveDB = get_Save_DB()

# qq
qidianCollection = mongoDB.novel_qidian
qidianSaveCollection = mongoSaveDB.novel_qidian


def updateNovel():
    global qidianCollection
    global qidianSaveCollection
    print('start qidian db update ')
    qidianSaveCollection.drop()
    qidian = qidianCollection.find()
    for one in qidian:
        try:
            qidianSaveCollection.insert(
                {'last_update_time': one['last_update_time'], 'update_time': one['update_time'],
                 'theme_tag': one['theme_tag'],
                 'status_tag': one['status_tag'], 'author': one['author'], 'title': one['title'],
                 'introduction': one['introduction'], 'author_defined_tag': one['author_defined_tag'],
                 'cover': one['cover'], 'recommend_count': one['recommend_count'], 'source': one['source'],
                 'url': one['url'], 'click_count': one['click_count'], 'words_count': one['words_count'],
                 'last_update_chapter': one['last_update_chapter'], 'week_award_count': one['week_award_count'],
                 'description': one['description']})

        except Exception as e:
            print("qidian Exception:", e)
            # mongoQQCollection.remove({'_id': ObjectId(one['_id'])})
            # for aa in mongoQQCollection.find({'_id': ObjectId(one['_id'])}):
    print('qidian db update finished')


if __name__ == "__main__":
    updateNovel()
